package com.aya.transaction.service;

import com.aya.transaction.entity.Blog;
import com.aya.transaction.mapper.BlogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BlogSimpleTransactionService {
    @Resource
    BlogMapper blogMapper;

    @Transactional
    public int updateById(String id, String content) {
        Blog blog = selectByPrimaryKey("1");
        System.out.println("child1 blog:"+blog);
        return blogMapper.updateById(id, content);
    }

    @Transactional
    public int updateByIdWithException(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("default exception");
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int updateByIdWithNotSupported(String id, String content) {
        return blogMapper.updateById(id, content);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int updateByIdWithNotSupportedException(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("not support exception");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateByIdRequiredFail(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("required exception");
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateByIdRequiredNew(String id, String content) {
        blogMapper.updateById(id, content);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateByIdRequiredNewFail(String id, String content)  {
        Blog blog = selectByPrimaryKey("1");
        System.out.println("child2 blog:"+blog);
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("requires exception");
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateByIdMandatory(String id, String content) {
        blogMapper.updateById(id, content);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateByIdMandatoryException(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("mondatory exception");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateByIdSupported(String id, String content) {
        int i = blogMapper.updateById(id, content);
    }

    @Transactional
    public void updateByIdErrorSql(String id, String content) throws Exception {
        int i = blogMapper.updateByIdErrorSql(id, content);
        throw new Exception("haha");
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateByIdSupportedException(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("supports exception");
    }

    @Transactional(propagation = Propagation.NEVER)
    public void updateByIdNever(String id, String content) {
        int i = blogMapper.updateById(id, content);
    }

    @Transactional(propagation = Propagation.NEVER)
    public void updateByIdNeverException(String id, String content) {
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("never exception");
    }

    @Transactional(propagation = Propagation.NESTED)
    public void updateByIdNested(String id, String content) {
        int i = blogMapper.updateById(id, content);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void updateByIdNestedException(String id, String content) {
        Blog blog = selectByPrimaryKey("1");
        System.out.println("nested child2 blog:"+blog);
        int i = blogMapper.updateById(id, content);
        throw new RuntimeException("nested exception");
    }

    @Transactional
    public int insertRequired(String id, String content) {
        int i = blogMapper.insertBlog(id, content);
        throw new RuntimeException("not support exception");
    }

    @Transactional
    public int insertRequiredException(String id, String content) {
        int i = blogMapper.insertBlog(id, content);
        throw new RuntimeException("not support exception");
    }

    public Blog selectByPrimaryKey(String id) {
        return blogMapper.selectTransBlog(id);
    }
}
